package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.CollectListAdapter;
import com.gears_software.sxdc.dccx.data.finder.IUsualAddressFinder;
import com.gears_software.sxdc.dccx.data.model.AddressList;
import com.gears_software.sxdc.dccx.data.model.ResultModel;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/7/26.
 * 常用地址
 */
@EActivity(R.layout.usual_address)
public class UsualAddressActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView tv_jia_icon;
    @ViewById
    TextView tv_gongsi_icon;
    @ViewById
    TextView tv_home_redact_icon;
    @ViewById
    TextView tv_firm_redact_icon;
    @ViewById
    TextView tv_home_address;
    @ViewById
    TextView tv_firm_address;
    @ViewById
    ListView lv_shoucang;
    @ViewById
    RelativeLayout rl_home;
    @ViewById
    RelativeLayout rl_gongsi;
    @RestService
    IUsualAddressFinder addressFinder;
    private CollectListAdapter adapter;
    List<AddressList> list = new ArrayList<>();
    private int requestCode = 3;
    AddressList addressList;
    String homeAddres = "";
    String companyAddress = "";
    int homeIndex;
    int companyIndex;

    @AfterViews
    void created() {
        actionbar_title.setText("常用地址");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        getAddressList();
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        //right_icon.setVisibility(View.GONE);
        right_icon.setText("添加");
        right_icon.setTextSize(18);
        tv_jia_icon.setTypeface(app.iconfont);
        tv_gongsi_icon.setTypeface(app.iconfont);
        tv_home_redact_icon.setTypeface(app.iconfont);
        tv_firm_redact_icon.setTypeface(app.iconfont);
    }

    @Click
    void back_icon() {
        this.finish();
    }

    //添加
    @Click
    void right_icon() {
        Intent intent = new Intent(this, CompileAddressActivity_.class);
        intent.putExtra("address", "3");
        intent.putExtra("flag", "add");
        startActivityForResult(intent, requestCode);
    }

    @Click
    void rl_home() {
        Intent intent = new Intent(this, CompileAddressActivity_.class);
        intent.putExtra("address", "1");
        if (homeAddres == null || homeAddres.equals("")) {
            intent.putExtra("flag", "add");
        } else {
            intent.putExtra("addressModel", list.get(homeIndex));
            intent.putExtra("flag", "gx");
        }
        startActivityForResult(intent, requestCode);
    }

    @Click
    void rl_gongsi() {
        Intent intent = new Intent(this, CompileAddressActivity_.class);
        intent.putExtra("address", "2");
        if (companyAddress == null || companyAddress.equals("")) {
            intent.putExtra("flag", "add");
        } else {
            intent.putExtra("addressModel", list.get(companyIndex));
            intent.putExtra("flag", "gx");
        }
        startActivityForResult(intent, requestCode);
    }

    @Background
    public void getAddressList() {
        ResultModel<List<AddressList>> res = addressFinder.getAddressList();
        showResout(res);
    }

    @UiThread
    void showResout(ResultModel<List<AddressList>> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                list = res.getData();
                if (list != null && list.size() > 0) {
                    List<AddressList> addressLists = new ArrayList<AddressList>();
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).getAddresstype() != null && !list.get(i).getAddresstype().equals("")) {
                            if (list.get(i).getAddresstype().equals("1")) {
                                homeIndex = i;
                                homeAddres = list.get(i).getDetailedaddress();
                                tv_home_address.setVisibility(View.VISIBLE);
                                tv_home_address.setText(homeAddres);
                            }
                            if (list.get(i).getAddresstype().equals("2")) {
                                companyIndex = i;
                                companyAddress = list.get(i).getDetailedaddress();
                                tv_firm_address.setVisibility(View.VISIBLE);
                                tv_firm_address.setText(companyAddress);
                            }
                            if (list.get(i).getAddresstype().equals("3")) {
                                addressLists.add(list.get(i));
                            }
                        } else {
                            tv_firm_address.setVisibility(View.GONE);
                            tv_home_address.setVisibility(View.GONE);
                        }
                    }
                    adapter = new CollectListAdapter(UsualAddressActivity.this, addressLists);
                    lv_shoucang.setAdapter(adapter);
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 3) {
            getAddressList();
        }
    }

}
